/**
 * MyExceptionHandler.java
 * com.ebook.util
 * Copyright (c) 2013, 北京微课创景教育科技有限公司版权所有.
*/

package com.ebook.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.dao.DataAccessException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.JsonParseException;

/**
 * springmvc异常处理类
 * @author   草帽
 * @Date	 2013-3-2 	 
 */
public class MyExceptionHandler implements HandlerExceptionResolver {

	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		String status = "success";
		// 根据不同错误输出不同错误提示 
		if (ex instanceof DataAccessException) {
			ex.printStackTrace();
			status = Status.REJECTED + ":" + "++++++  DataAccessException  ++++++";
		} else if (ex instanceof JsonParseException) {
			ex.printStackTrace();
			status = Status.REJECTED + ":" + "++++++  JsonParseException  ++++++";
		} else if (ex instanceof IOException) {
			ex.printStackTrace();
			status = Status.REJECTED + ":" + "++++++  IOException  ++++++";
		} else {
			ex.printStackTrace();
			status = Status.REJECTED + ":" +  "++++++  OtherException  ++++++";
		}
		modelMap.put("status", status);
		return new ModelAndView().addAllObjects(modelMap);
		
	}
}